﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;

namespace SsrsConverter
{
    public class DataSource
    {
        private static XNamespace GetXmlns(XElement element)
        {
            XNamespace xmlns = (string)element.Document.Root.Attribute("xmlns");
            return xmlns == null ? string.Empty : xmlns;
        }

        private static bool HasElement(XElement element, string name)
        {
            XNamespace xmlns = GetXmlns(element);
            return element.Element(GetXmlns(element) + name) != null;
        }

        public static DataSource Parse(XElement element)
        {
            string name = (string)element.Attribute("Name");
            DataSource ds = new DataSource(name);

            XNamespace xmlns = GetXmlns(element);

            if (HasDataSourceReference(element))
            {
                element = LoadDataSourceReference((string)element.Element(xmlns + "DataSourceReference"));
                xmlns = GetXmlns(element);
            }

            if (HasConnectionProperties(element))
                ParseConnectionProperties(ds, element.Element(xmlns + "ConnectionProperties"));

            return ds;
        }

        private static XElement LoadDataSourceReference(string path)
        {
            // TODO: Very primitive solution.

            if (!File.Exists(path))
                path += ".rds";

            XDocument xdoc = XDocument.Load(path);
            return xdoc.Root;
        }

        private static bool HasConnectionProperties(XElement element)
        {
            return HasElement(element, "ConnectionProperties");
        }

        private static bool HasDataSourceReference(XElement element)
        {
            return HasElement(element, "DataSourceReference");
        }

        private static void ParseConnectionProperties(DataSource ds, XElement element)
        {
            XNamespace xmlns = GetXmlns(element);

            string dataProvider = (string)element.Element(xmlns + "DataProvider");
            if (dataProvider == null)
                dataProvider = (string)element.Element(xmlns + "Extension");    // In .rds file
            string connectString = (string)element.Element(xmlns + "ConnectString");
            bool? integratedSecurity = (bool?)element.Element(xmlns + "IntegratedSecurity");

            ds.SetDataProvider(dataProvider);
            ds.SetConnectString(connectString);
            ds.SetIntegratedSecurity(integratedSecurity == null ? false : (bool)integratedSecurity);
        }

        private DataSource(string name)
        {
            this.name = name;
        }

        private string name;
        public string GetName()
        {
            return name;
        }

        private string dataProvider;
        public string GetDataProvider()
        {
            return dataProvider;
        }
        private void SetDataProvider(string dataProvider)
        {
            this.dataProvider = dataProvider;
        }

        private string connectString;
        public string GetConnectString()
        {
            return connectString;
        }
        private void SetConnectString(string connectString)
        {
            this.connectString = connectString;
        }

        private bool integratedSecurity;
        public bool GetIntegratedSecurity()
        {
            return integratedSecurity;
        }
        private void SetIntegratedSecurity(bool integratedSecurity)
        {
            this.integratedSecurity = integratedSecurity;
        }
    }
}
